
import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import { useUserStore } from '@/stores'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 进度条下面的小圆圈关闭掉
NProgress.configure({
  showSpinner: false
})


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  
  //createWebHistory()参数默认是"/"
  routes: [
    { path: '/login', 
    component: Login,
    meta: { title: '登录' },
    name:'login'
  },

   {
    path:'/',
    component:()=>import('@/views/Layout/index.vue'),
    redirect: '/home',
    children:[
      {
        path:'/notify',
        meta:{title:'消息中心'},
        component:()=>import('@/views/Notify/index.vue')
       },
       {
        path:'/home',
        meta:{title:'首页'},
        component:()=>import('@/views/Home/index.vue')
       },
       {
        path:'/user',
        meta:{title:'我的'},
        component:()=>import('@/views/User/index.vue')
       },
       {
        path:'/article',
        meta:{title:'健康百科'},
        component:()=>import('@/views/Article/index.vue')
       },
       {
        path: '/user/patient',
        component: () => import('@/views/User/PatientPage.vue'),
        meta: { title: '家庭档案' }
      },
       
    ]
   },
  
   
  ]
})

router.beforeEach((to)=>{
  //开启进度条
  NProgress.start()
  // 从pina查询token 如果没有token并且访问的页面不是白名单中的页面那么就跳转到login页面
  const store = useUserStore()
  // 设置白名单
  const whiteList = ['/login','/register','/login/callback']
   // 如果没有登录且不在白名单内，去登录
  if(!store.user?.token && !whiteList.includes(to.path)) return '/login' 
   
})

router.afterEach((to) => {
  document.title = `${to.meta.title || ''}-优医问诊`
  //关闭进度条
  NProgress.done()
})


export default router
